package HelperFinancial.DataBase;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.test.IsolatedContext;

public class AlertAdapter {
	
	private AlertHelper BDHelper;
	
	private static AlertAdapter instance;

	private AlertAdapter(Context context) {
		this.BDHelper = new AlertHelper(context);
		
	}
	
	public void populate(){
		if(getAll().size() == 0){
			BDHelper.populate();
		}
	}
	
	public static AlertAdapter getInstance(Context context){
		if(instance == null){
			instance = new AlertAdapter(context);
		}
		
		return instance;
	}
	
	public static void destroy() {
		if(instance != null){
			instance.close();
		}
		instance = null;
	}
	
	private void close() {
		BDHelper.close();
	}

	
	public int getId(){
		Cursor cr = BDHelper.getId();
		int colId = cr.getColumnIndex("_id");
		
		
		cr.moveToFirst();
		
		
		return cr.getInt(colId);
	}
	
	public String getBalance(){
		Cursor cr = BDHelper.getBalanceNegative();
		int colbalance = cr.getColumnIndex("balance_negative");
		
		
		cr.moveToFirst();
		
		
		return cr.getString(colbalance);
	}
	
	public String getDebits(){
		Cursor cr = BDHelper.getViewDebits();
		int colbalance = cr.getColumnIndex("debits");
		
		
		cr.moveToFirst();
		
		
		return cr.getString(colbalance);
	}
	
	public void updateNegativeBalance(String value){
		BDHelper.changeNegativeBalance(getId(), value);
	}
	
	public void updateViewDebits(String value){
		BDHelper.changeViewDebits(getId(), value);
	}
	
	public void removeAll(){
		BDHelper.removeAll();
	}
	
	public List<String> getAll() {
		Cursor cr = BDHelper.getAll();
		List<String> allCounts = new ArrayList<String>();
		int colId = cr.getColumnIndex("_id");
		int colBalance = cr.getColumnIndex("balance_negative");
		int colDebit = cr.getColumnIndex("debits");
		
		
		while(cr.moveToNext()){
			allCounts.add(cr.getString(colBalance) + "|" + cr.getString(colDebit));
		}
		return allCounts;
	}

}
